|
|
|
|
|
|
|
Call RtlMoveMemory(length, ByVal (StrPtr(b$) - 4), 4)
Debug.Print "Length: " & length
End Sub |
|
|
|
|
|
|
|
|
String b$ contains the text ''Hello". We have a dynamic array a() that is loaded with the contents of the string using the assignment a() = b$. This assignment, which is unfamiliar to many Visual Basic programmers, is an easy way to copy data between strings and dynamic byte arrays without any conversion process. The dynamic array is automatically assigned to the correct length. The function first displays the value of each byte in hexadecimal. As you can see, the data corresponds to the values shown in Figure T5-5. |
|
|
|
|
|
|
|
|
Next, we use the same techniques shown earlier to copy data from memory. In this case, the StrPtr function is used to obtain the pointer to the BSTR (VarPtr would return a pointer to the variable that contains the pointer to the BSTR). We subtract 4 from the pointer value to point to the string length, and copy that value into a Long variable named length. The results are as follows: |
|
|
|
|
|
|
|
|
Inside Hello
0 48
1 0
2 65
3 0
4 6C
5 0
6 6C
7 0
8 6F
9 0
Length: 10 |
|
|
|
|
|
|
|
|
A C string, also referred to as a NULL-terminated string, is shown in the lower part of Figure T5-5. This type of string is called a C string because it is the standard string format used by the C and C++ programming languages. It is called a NULL-terminated string because the end of the string is marked by the presence of a NULL character. Note that the end of the string is marked by a NULL character, not necessarily a 0 byte. With an ANSI string, which contains one byte per character, the NULL character is a single byte containing the value 0 at the end of the string. With a Unicode string, the NULL character is two bytes long, just like any other character. Both bytes of the NULL character have a value of 0. |
|
|
|
|
|